package cn.springcloud.fix.quartz.service;

import cn.springcloud.fix.common.core.base.BaseResponse;
import cn.springcloud.fix.quartz.api.request.CreateJobRequest;
import cn.springcloud.fix.quartz.api.request.ListJobRequest;
import cn.springcloud.fix.quartz.api.request.TriggerJobRequest;
import cn.springcloud.fix.quartz.api.request.UpdateJobRequest;
import cn.springcloud.fix.quartz.api.response.ListJobResponse;
import cn.springcloud.fix.quartz.config.QuartzManager;
import cn.springcloud.fix.quartz.dao.entity.SysJob;
import cn.springcloud.fix.quartz.dao.mapper.SysJobMapper;
import cn.springcloud.fix.quartz.util.QuartzUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private SysJobMapper sysJobMapper;

    @Override
    public ListJobResponse getJobs(ListJobRequest request) {
        ListJobResponse response = new ListJobResponse();
        List<SysJob> sysJobList = sysJobMapper.selectList(null);
        if (CollectionUtils.isEmpty(sysJobList)) return response;
        for (SysJob source : sysJobList) {
            ListJobResponse.JobDefinition dest = new ListJobResponse.JobDefinition();
            BeanUtils.copyProperties(source, dest);
            response.getJobList().add(dest);
        }
        return response;
    }

    @Override
    public List<? extends Trigger> getTriggers(String a) {
        return QuartzUtil.getJob(scheduler, a, "job_group1");
    }

    @Override
    public BaseResponse triggerJob(TriggerJobRequest request) {
        try {
            QuartzUtil.triggerJob(scheduler, request.getTaskId(), request.getTaskGroup());
            return new BaseResponse();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new BaseResponse(-1, e.getMessage());
        }
    }

    @Override
    public BaseResponse createJob(CreateJobRequest request) {
        try {
            SysJob sysJob = sysJobMapper.selectById(request.getJobId());
            if (sysJob != null) return new BaseResponse(-1, "已存在该定时任务！");

            SysJob tmp = new SysJob();
            tmp.setId(request.getJobId());
            tmp.setName(request.getJobName());
            tmp.setJobGroup(request.getJobGroup());
            tmp.setCronExpression(request.getCronExpression());
            tmp.setJobStatus(request.getJobStatus());
            LocalDateTime localDateTime = LocalDateTime.now();
            tmp.setCreateTime(localDateTime);
            tmp.setUpdateTime(localDateTime);
            int res = sysJobMapper.insert(tmp);
            if (res != 1) return new BaseResponse(-1, "插入定时任务失败！");
            //QuartzUtil.createScheduleJob(scheduler, QuartzManager.class, tmp.getId(), tmp.getJobGroup(), tmp.getCronExpression());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new BaseResponse(-1, e.getMessage());
        }
        return new BaseResponse(-1, "操作失败");
    }

    @Override
    public void reBuildJobTask(String taskId, String group, String cronExpression) throws SchedulerException {
        QuartzUtil.deleteJob(scheduler, taskId, group);
        QuartzUtil.createScheduleJob(scheduler, QuartzManager.class, taskId, group, cronExpression);
    }

    @Override
    public BaseResponse updateJob(UpdateJobRequest request) {
        try {
            SysJob sysJob = sysJobMapper.selectById(request.getId());
            if (sysJob == null) return new BaseResponse(-1, "不存在该定时任务！");

            QuartzUtil.updateScheduleJob(scheduler, sysJob.getId(), sysJob.getJobGroup(), request.getCronExpression());

            SysJob tmp = new SysJob();
            tmp.setId(sysJob.getId());
            tmp.setCronExpression(request.getCronExpression());
            tmp.setUpdateTime(LocalDateTime.now());
            int res = sysJobMapper.updateById(tmp);
            if (res == 1) return new BaseResponse();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new BaseResponse(-1, e.getMessage());
        }
        return new BaseResponse(-1, "操作失败");
    }

    @Override
    public BaseResponse deleteJob(String id) {
        try {
            if (id == null) return new BaseResponse(-1, "缺少id参数");
            SysJob sysJob = sysJobMapper.selectById(id);
            if (sysJob == null) return new BaseResponse(-1, "不存在该定时任务！");
            SysJob tmp = new SysJob().setId(id).setJobStatus(-1).setUpdateTime(LocalDateTime.now());
            sysJobMapper.updateById(tmp);
            QuartzUtil.deleteJob(scheduler, id, sysJob.getJobGroup());
            return new BaseResponse();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new BaseResponse(-1, e.getMessage());
        }
    }

    @Override
    public BaseResponse pauseJob(String id) {
        try {
            if (id == null) return new BaseResponse(-1, "缺少id参数");
            SysJob sysJob = sysJobMapper.selectById(id);
            if (sysJob == null) return new BaseResponse(-1, "不存在该定时任务！");
            SysJob tmp = new SysJob().setId(id).setJobStatus(0).setUpdateTime(LocalDateTime.now());
            sysJobMapper.updateById(tmp);
            QuartzUtil.pauseJob(scheduler, id, sysJob.getJobGroup());
            return new BaseResponse();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new BaseResponse(-1, e.getMessage());
        }
    }

    @Override
    public BaseResponse resumeJob(String id) {
        try {
            if (id == null) return new BaseResponse(-1, "缺少id参数");
            SysJob sysJob = sysJobMapper.selectById(id);
            if (sysJob == null) return new BaseResponse(-1, "未找到该定时任务！");
            SysJob tmp = new SysJob().setId(id).setJobStatus(1).setUpdateTime(LocalDateTime.now());
            sysJobMapper.updateById(tmp);
            QuartzUtil.resumeJob(scheduler, id, sysJob.getJobGroup());
            return new BaseResponse();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new BaseResponse(-1, e.getMessage());
        }
    }
}
